const connection = require('../app/database')

class WeblogService {
  // 创建文章
  async create(title, content, userId) {
    const statement = `INSERT INTO weblog (title, content, user_id) VALUES (?, ?, ?);`
    const [ result ] = await connection.execute(statement, [title, content, userId])
    return result
  }
  // 获取文章（全部）
  async list() {
    const statement = `SELECT * FROM weblog;`
    const [ result ] = await connection.execute(statement)
    return result
  }
  // 获取文章（单个）
  async item(weblogId) {
    const statement = `SELECT * FROM weblog WHERE id = ?;`
    const [ result ] = await connection.execute(statement, [weblogId])
    return result
  }
  // 删除文章
  async remove(weblogId) {
    const statement = `DELETE FROM weblog WHERE id = ?;`
    const [ result ] = await connection.execute(statement, [weblogId])
    return result
  }
  // 更新文章
  async update(title, content, weblogId) {
    const statement = `UPDATE weblog SET title = ?, content = ? WHERE id = ?;`
    const [ result ] = await connection.execute(statement, [title, content, weblogId])
    return result
  }
}

module.exports = new WeblogService()